package com.uton.rfidcontroller.view.activity.write.prescan;

import android.content.Intent;

import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.common.intent.IntentBuilder;
import com.uton.rfidcontroller.common.rfid.UfhData;
import com.uton.rfidcontroller.common.utils.LogUtil;
import com.uton.rfidcontroller.common.utils.Utils;
import com.uton.rfidcontroller.databinding.ActivityFinalWriteBinding;
import com.uton.rfidcontroller.view.activity.SettingActivity;
import com.uton.rfidcontroller.view.base.BaseActivity;
import com.uton.rfidcontroller.viewmodel.activity_model.FinalWriteActivityViewModel;

/**
 * Created by 王家宝 on 2017/10/16.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public class FinalWriteActivity extends BaseActivity<ActivityFinalWriteBinding, FinalWriteActivityViewModel> {

    public static final int STANDARD_LENGTH = 24;
    public static final int WRITE_SUCCESS = 0;
    public static final int WRITE_FAILED = -1;
    private boolean isWriteInSuccess;
    private String tidNum;


    @Override
    protected int setLayout() {
        return R.layout.activity_final_write;
    }

    @Override
    protected void inject() {
        getBinding().setModel(getViewModel());
    }

    @Override
    protected String setTitle() {
        return getString(R.string.ui_input);
    }

    @Override
    protected void doSomething() {
        getViewModel().cardId.set(UfhData.getUfh_id());
    }

    @Override
    protected void doOnFinish() {
        super.doOnFinish();
        Intent intent = new Intent();
        intent.putExtra(GlobalConstants.IS_WRITE_IN_SUCCESS, isWriteInSuccess);
        intent.putExtra(GlobalConstants.TID_NUM, tidNum);
        setResult(GlobalConstants.RESULT_IS_WRITE_SUCCESS, intent);
    }

    @Override
    protected FinalWriteActivityViewModel setViewModel() {
        return new FinalWriteActivityViewModel(
                getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM1)) {
            @Override
            public void writeIn() {
                if (tidNum != null && tidNum.length() != 0) {

                    if (!isWriteInSuccess) {
                        //判断是否开启扫描
                        if (!UfhData.isDeviceOpen()) {
                            Utils.showShortToast(getString(R.string.toast_not_open_device));
                            startActivity(new IntentBuilder().from(FinalWriteActivity.this, SettingActivity.class).getIntent());
                            return;
                        }
                        String rfidCardId = getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM1);
                        if (rfidCardId != null && rfidCardId.length() != STANDARD_LENGTH) {
                            return;
                        }

                        int result = UfhData.UhfGetData.Write6c(
                                (byte) 6, (byte) (UfhData.epcBytes.get(UfhData.getUfh_id()).length / 2),
                                UfhData.epcBytes.get(UfhData.getUfh_id()), (byte) 1, (byte) 2,
                                UfhData.UhfGetData.hexStringToBytes(rfidCardId),
                                UfhData.UhfGetData.hexStringToBytes("00000000"));

                        LogUtil.d("写入的结果是：" + result);

                        String resultMsg;
                        if (result == WRITE_SUCCESS) {
                            resultMsg = "写入成功";
                            isWriteInSuccess = true;
                        } else if (result == WRITE_FAILED) {
                            resultMsg = "写入失败";
                            isWriteInSuccess = false;
                        } else {
                            resultMsg = "未知错误";
                            isWriteInSuccess = false;
                        }

                        getViewModel().writeInState.set(resultMsg);
                    } else {
                        Utils.showShortToast("已成功写入，不能再次写入");
                    }

                } else {
                    Utils.showShortToast("请先获取到TID码");
                }

            }

            @Override
            public void readTid() {
                if (tidNum == null || tidNum.length() == 0) {
                    if (!UfhData.isDeviceOpen()) {
                        Utils.showShortToast(getString(R.string.toast_not_open_device));
                        startActivity(new IntentBuilder().from(FinalWriteActivity.this, SettingActivity.class).getIntent());
                        return;
                    }

                    UfhData.UhfGetData.Read6C((byte) (UfhData.epcBytes.get(getViewModel().cardId.get()).length / 2),
                            UfhData.epcBytes.get(getViewModel().cardId.get()),
                            (byte) 2,
                            Byte.valueOf("0"),
                            Byte.valueOf("8"),
                            UfhData.UhfGetData.hexStringToBytes("00000000"));
                    String temp = UfhData.UhfGetData.bytesToHexString(UfhData.UhfGetData.getRead6Cdata(), 0, UfhData.UhfGetData.getRead6CLen()).toUpperCase();
                    int m = UfhData.UhfGetData.getRead6CLen();
                    if (m == 0) {
                        tidNum = "";
                        getViewModel().tidState.set("获取TID失败");
                    } else {
                        tidNum = temp.toUpperCase();
                        getViewModel().tidState.set("获取TID成功");
                    }
                    LogUtil.d("获取到的TID码" + tidNum);
                } else {
                    Utils.showShortToast("已获取到TID码");
                }

            }
        };
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(GlobalConstants.IS_WRITE_IN_SUCCESS, isWriteInSuccess);
        intent.putExtra(GlobalConstants.TID_NUM, tidNum);
        setResult(GlobalConstants.RESULT_IS_WRITE_SUCCESS, intent);
        finish();
    }
}
